package com.sky.utils;


import com.alibaba.fastjson.JSONObject;
import com.sky.properties.WeChatProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Slf4j
//@AllArgsConstructor
@Data
@Component
public class WeChatLoginUtil {

    @Autowired
    WeChatProperties properties;

    private final String WEIXIN_API = "https://api.weixin.qq.com/sns/jscode2session";//微信登录地址
    private static final String GRANT_TYPE = "authorization_code";

    public String getLogin(String code) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", properties.getAppid());
        params.put("secret", properties.getSecret());
        params.put("js_code", code);
        params.put("grant_type", GRANT_TYPE);
        String json = HttpClientUtil.doGet(WEIXIN_API, params);
        JSONObject jsonObject = JSONObject.parseObject(json);
        String openId = jsonObject.getString("openid");

        return openId;
    }
}
